﻿using System;
using NUnit.Framework;
using WWB.DisqusSharp.Model.DisqusService;

namespace WWB.DisqusSharp.Tests.TestUtility
{
    public static class DisqusServiceResultTestExtension
    {
        public static IDisqusServiceResult IsSuccessfulResult(this IDisqusServiceResult result)
        {
            Assert.That(result.IsSuccessful, "Result was not successful. Error was " + (result.Error ?? ServiceError.NoError).ToString());
            return result;
        }

        public static IDisqusServiceResult IsNotSuccessfulResult(this IDisqusServiceResult result)
        {
            Assert.That(result.IsError, "Result was successufl! Result is " + result.JsonPayload);
            return result;
        }

        public static IDisqusServiceResult<T> IsSuccessfulResult<T>(this IDisqusServiceResult<T> result)
        {
            Assert.That(result.IsSuccessful, "Result was not successful. Error was " + (result.Error ?? ServiceError.NoError).ToString());
            return result;
        }

        public static IDisqusServiceResult<T> IsNotSuccessfulResult<T>(this IDisqusServiceResult<T> result)
        {
            Assert.That(result.IsError, "Result was successufl! Result is " + result.JsonPayload);
            return result;
        }

        public static IDisqusServiceResult<T> ResultMatching<T>(this IDisqusServiceResult<T> result, Func<T, bool> predicate)
        {
            if (!predicate(result.Payload))
            {
                Assert.Fail("Payload did mot match predicate");
            }
            return result;
        }

        public static IDisqusServiceResult<T> ResultNotMatching<T>(this IDisqusServiceResult<T> result, Func<T, bool> predicate)
        {
            if (predicate(result.Payload))
            {
                Assert.Fail("Payload was matched!");
            }
            return result;
        }

        public static IDisqusServiceResult<T> WithoutPayload<T>(this IDisqusServiceResult<T> result)
        {
            Assert.That(result.Payload, Is.Null, "Payload for " + result.ToString() + " was not null.");
            return result;
        }

        public static T WithPayload<T>(this IDisqusServiceResult<T> result)
        {
            Assert.That(result.Payload, Is.Not.Null, "Payload for " + result.ToString() + " was null.");
            return result.Payload;
        }

        public static ServiceError WithError(this IDisqusServiceResult result)
        {
            Assert.That(result.Error, Is.Not.Null, "Error not found");
            return result.Error;
        }
    }

}

